#Android needs this
#export SHELL=/system/bin/sh

PRG            = vbi_inserter

# List Assembler source files here.
#     Make them always end in a capital .S.  Files ending in a lowercase .s
#     will not be considered source files but generated files (assembler
#     output from the compiler), and will be deleted upon "make clean"!
#     Even though the DOS/Win* filesystem matches both .s and .S the same,
#     it will preserve the spelling of the filenames, and gcc itself does
#     care about how the name is spelled on its command-line.
ASRC = 
#gcrt1.S

#OBJ            =  node.o
#OBJ            =  xbee.c uart.c suart.c gps.c tsync.c node.c
OBJ            =  $(ASRC:%.S=%.o) vbi_inserter.o
#OBJ            =  $(ASRC:%.S=%.o) uart.o xbee.o node.o

MCU_TARGET     = atmega328p


#  Strip unused code -> -ffunction-sections -fdata-sections 
#                       + linker option --gc-sections
#OPTIMIZE       = -g -O
OPTIMIZE       = -g -Os -ffunction-sections -fdata-sections -funsigned-bitfields \
				-fpack-struct -fshort-enums
#OPTIMIZE       += -flto -fwhole-program

# mag der gcc 4.6.2 nicht
#OPTIMIZE       += -maccumulate-args

# dump gimple
#OPTIMIZE += -fdump-tree-gimple


F_CPU            = 16000000

DEFS           = -DF_CPU=$(F_CPU) 
#-DDEBUG
#-DDEBUG
# gcc is needed when using -nostdlib -nodefaultlibs
#LIBS           = -lgcc

# Display size of file.
ELFSIZE = avr-size --mcu=$(MCU_TARGET) --format=avr $(PRG).elf 

# You should not have to change anything below here.

CC             = avr-gcc

# Override is only needed by avr-lib build system.

override CFLAGS        = -Wall -Wextra $(OPTIMIZE) -std=gnu99 -mmcu=$(MCU_TARGET) $(DEFS)
override LDFLAGS       = -Wl,--as-needed -Wl,--relax -Wl,--gc-sections,-Map,$(PRG).map
#LDFLAGS                += -fno-fat-lto-objects
ALL_ASFLAGS            = -mmcu=$(MCU_TARGET) -I. -x assembler-with-cpp $(ASFLAGS)

OBJCOPY        = avr-objcopy
OBJDUMP        = avr-objdump

all: $(PRG).elf lst text eeprom sizeafter

$(PRG).elf: $(OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)

# this builds the whole elf in a single command
#$(PRG).elf:
#	$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o $@ $(LIBS)


# Assemble: create object files from assembler source files.
.S.o:
	$(CC) -c $(ALL_ASFLAGS) $< -o $@

# dependency:
vbi_inserter.o: vbi_inserter.c

#TimeSync.o: TimeSync.c
sizeafter:
	$(ELFSIZE)

#	avr-size -C --mcu=atmega328p node.elf
#	avr-size -C --mcu=atmega328p node.elf
#	@if test -f $(PRG).hex; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
#	@if test -f $(PRG).hex; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
#	2>/dev/null; echo; fi 

clean:
	rm -rf *.o $(PRG).elf *.eps *.png *.pdf *.bak *.gimple *.bin
	rm -rf *.lst *.map 
	rm -rf $(EXTRA_CLEAN_FILES)

lst:  $(PRG).lst

%.lst: %.elf
	$(OBJDUMP) -h -S $< > $@

# Rules for building the .text rom images

text: hex bin srec

hex:  $(PRG).hex
bin:  $(PRG).bin
srec: $(PRG).srec

%.hex: %.elf
	$(OBJCOPY) -j .text -j .data -O ihex $< $@

%.srec: %.elf
	$(OBJCOPY) -j .text -j .data -O srec $< $@

%.bin: %.elf
	$(OBJCOPY) -j .text -j .data -O binary $< $@

# Rules for building the .eeprom rom images

eeprom: ehex ebin esrec

ehex:  $(PRG)_eeprom.hex
ebin:  $(PRG)_eeprom.bin
esrec: $(PRG)_eeprom.srec

%_eeprom.hex: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ \
	|| { echo empty $@ not generated; exit 0; }

%_eeprom.srec: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@ \
	|| { echo empty $@ not generated; exit 0; }

%_eeprom.bin: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@ \
	|| { echo empty $@ not generated; exit 0; }

# Every thing below here is used by avr-libc's build system and can be ignored
# by the casual user.

FIG2DEV                 = fig2dev
EXTRA_CLEAN_FILES       = *.hex *.bin *.srec *.[ch]~ Makefile~

dox: eps png pdf

eps: $(PRG).eps
png: $(PRG).png
pdf: $(PRG).pdf

%.eps: %.fig
	$(FIG2DEV) -L eps $< $@

%.pdf: %.fig
	$(FIG2DEV) -L pdf $< $@

%.png: %.fig
	$(FIG2DEV) -L png $< $@
